# -*- coding: utf-8 -*-
"""
@Author: 苏苏少年
@File: 爬取百度图片.py
@Time: 2022/4/10 15:46
"""

"""
    @@完整案例!!
    
"""

import requests # 请求包
import re   # 正则表达式
# 正则表达式是一个特殊的字符序列，它能帮助你方便的检查一个字符串是否与某种模式匹配。(匹配数据!)


# url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=8588205700455966117&ipn=rj&ct=201326592&is=&fp=result&fr=&word=%E5%B0%8F%E5%A7%90%E5%A7%90&queryWord=%E5%B0%8F%E5%A7%90%E5%A7%90&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=&pn=30&rn=30&gsm=1e&1649575839308='

# 右键网址!
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=8176760259026828195&ipn=rj&ct=201326592&is=&fp=result&fr=&word=%E5%B0%8F%E5%A7%90%E5%A7%90&queryWord=%E5%B0%8F%E5%A7%90%E5%A7%90&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=&pn=60&rn=30&gsm=3c&1649579558916='

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}

res = requests.get(url=url,headers=headers)
print(res.text)

#  匹配数据,获取图片网址!
img_url_list = re.findall('"thumbURL":"(.*?)",',res.text)
# print(img_url_list)

# 这里我们是把数据流写入到文件夹中
num = 0
for img_url in img_url_list:
    result = requests.get(img_url,headers=headers)
     # 数据流(图片在网络中的传播形式!)
    # print(result.content)
    with open('img/' + str(num) + ".jpg", 'wb')as f:
        f.write(result.content)
    num +=1








